<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="缴费标题" prop="payName">
        <el-input
          v-model="queryParams.payName"
          placeholder="请输入缴费标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推送类型" prop="payType">
        <el-select
          v-model="queryParams.payType"
          placeholder="请选择推送类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.f_pd_push_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推送状态" prop="pushStatus">
        <el-select
          v-model="queryParams.pushStatus"
          placeholder="请选择推送状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.f_pd_push_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="进行状态" prop="progressStatus">
        <el-select
          v-model="queryParams.progressStatus"
          placeholder="请选择进行状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.f_pd_ongoing_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="deptId">
        <!-- <el-input
          v-model="queryParams.deptId"
          placeholder="请输入年级id"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-cascader
          clearable
          :props="props"
          v-model="queryParams.deptId"
          popper-class="pc-sel-area-cascader"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item label="缴费截至日期" label-width="200">
        <el-date-picker
          v-model="daterangeEndTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['money:pay:edit']"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="payList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="序号"
        type="index"
        :index="table_index"
        width="50"
        align="center"
      >
      </el-table-column>
      <el-table-column label="缴费标题" align="center" prop="payName" />
      <el-table-column
        label="图片"
        align="center"
        prop="payPicture"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.payUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="缴费金额" align="center" prop="payMoney" />
      <el-table-column label="推送年级" align="center" prop="remark" />
      <el-table-column label="推送类型" align="center" prop="payType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.f_pd_push_type"
            :value="scope.row.payType"
          />
        </template>
      </el-table-column>
      <el-table-column label="推送状态" align="center" prop="pushStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.f_pd_push_status"
            :value="scope.row.pushStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="进行状态" align="center" prop="progressStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.f_pd_ongoing_status"
            :value="scope.row.progressStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="缴费截至日期"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="缴费量" align="center" prop="payNumber">
        <template slot-scope="scope">
          <div @click="haddle(scope.row)">{{ scope.row.payNumber }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleContent(scope.row)"
            v-hasPermi="['money:pay:query']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-show="scope.row.progressStatus != 1"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['money:pay:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['money:pay:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改缴费(推送服务模块)对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="780px"
      append-to-body
      align="center"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="收费标题" prop="payName">
              <el-input v-model="form.payName" placeholder="请输入收费标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收费金额" prop="payMoney">
              <el-input-number v-model="form.payMoney" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- <el-form-item label="年级id" prop="deptId">
              <el-input v-model="form.deptId" placeholder="请输入年级id" />
            </el-form-item> -->
            <el-form-item label="选择年级" prop="deptId">
              <!-- <el-input
          v-model="queryParams.deptId"
          placeholder="请输入年级id"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
              <el-cascader
                clearable
                :props="props"
                v-model="form.deptId"
                popper-class="pc-sel-area-cascader"
              >
              </el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推送类型" prop="payType">
              <el-select
                clearable
                v-model="form.payType"
                placeholder="请选择推送类型"
              >
                <el-option
                  v-for="dict in dict.type.f_pd_push_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="推送状态" prop="pushStatus">
              <el-select
                clearable
                v-model="form.pushStatus"
                placeholder="请选择推送状态"
              >
                <el-option
                  v-for="type in types"
                  :key="type.value"
                  :label="type.label"
                  :value="type.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="截至日期" prop="endTime">
              <el-date-picker
                clearable
                v-model="form.endTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择缴费截至日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col>
            <el-form-item label="图片">
              <image-upload v-model="form.payPicture" :limit="1" />
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="收费内容" prop="payContent">
              <editor v-model="form.payContent" :min-height="192" />
            </el-form-item>
          </el-col>
          <!-- 
          

          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请输入内容"
            />
          </el-form-item>

         
 -->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--查看公告内容-->
    <el-dialog :visible.sync="openContent" width="780px" append-to-body>
      <el-form ref="form" :model="form">
        <el-row>
          <el-col :span="24">
            <el-form-item>
              <el-tag size="medium" style="font-size: 15px">{{
                form.payName
              }}</el-tag>
            </el-form-item>
          </el-col>

          <el-col :span="24" style="margin-top: -22px">
            <el-divider
              ><i class="el-icon-star-off" style="margin-right: 3px"></i
              >缴费标题</el-divider
            >
          </el-col>
          <el-col :span="24" style="margin-top: 10px">
            <el-form-item>
              <el-card>
                <el-col>
                  <div
                    v-html="form.payContent"
                    class="content demo-image__lazy"
                  ></div>
                </el-col>
              </el-card>
            </el-form-item>
          </el-col>
          <el-col :span="24" style="margin-top: -22px">
            <el-divider
              ><i class="el-icon-star-off" style="margin-right: 3px"></i
              >缴费内容</el-divider
            >
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listPay, getPay, delPay, addPay, updatePay } from "@/api/money/pay";
import { studentClass } from "@/api/class/roster";
export default {
  name: "Pay",
  dicts: ["f_pd_push_type", "f_pd_push_status", "f_pd_ongoing_status"],
  data() {
    return {
      types: [
        { value: "0", label: "推送" },
        { value: "1", label: "不推送" },
      ],
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          const { level } = node;
          let parentId;
          if (node.value) {
            parentId = node.value;
          } else {
            parentId = 0;
          }
          studentClass({ parentId: parentId }).then((res) => {
            res.data.unshift({ deptId: -1, typeName: "全部年级" });
            const nodes = res.data.map((v) => ({
              value: v.deptId,
              label: v.typeName,
              leaf: level === 0,
            }));
            resolve(nodes);
          });
        },
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 缴费(推送服务模块)表格数据
      payList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openContent: false,
      // 缴费截至日期时间范围
      daterangeEndTime: [],
      // 缴费截至日期时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        payName: undefined,
        payType: undefined,
        pushStatus: undefined,
        progressStatus: undefined,
        deptId: undefined,
        endTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        payName: [
          { required: true, message: "缴费标题不能为空", trigger: "blur" },
        ],
        payContent: [
          { required: true, message: "缴费内容不能为空", trigger: "blur" },
        ],
        payMoney: [
          { type: "number", min: 0, message: "金额不能为0", trigger: "blur" },
        ],
        payType: [
          {
            required: true,
            message: "缴费推送类型不能为空",
            trigger: "change",
          },
        ],
        pushStatus: [
          {
            required: true,
            message: "缴费推送状态不能为空",
            trigger: "change",
          },
        ],
        deptId: [{ required: true, message: "年级不能为空", trigger: "blur" }],
        endTime: [
          { required: true, message: "缴费截至日期不能为空", trigger: "blur" },
        ],
        payContent: [
          { required: true, message: "缴费内容不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询缴费(推送服务模块)列表 */
    getList() {
      // console.log(this.queryParams);
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeEndTime && "" != this.daterangeEndTime) {
        this.queryParams.params["beginEndTime"] = this.daterangeEndTime[0];
        this.queryParams.params["endEndTime"] = this.daterangeEndTime[1];
      }
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] =
          this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listPay(this.queryParams).then((response) => {
        this.payList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //序号换页问题
    table_index(index) {
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1
      );
    },
    // 富文本校验
    contentRule() {
      this.$refs.addModelRef.validateField("payContent");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        payId: undefined,
        payName: undefined,
        payContent: undefined,
        payPicture: undefined,
        payMoney: "0",
        remark: undefined,
        payType: undefined,
        pushStatus: undefined,
        payUrl: undefined,
        payNumber: undefined,
        deptId: undefined,
        endTime: undefined,
        isDelete: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 查询公告内容按钮操作 */
    handleContent(row) {
      this.reset();
      const payId = row.payId || this.ids;
      getPay(payId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.openContent = true;
        this.title = "缴费详细信息";
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (this.queryParams.deptId != undefined) {
        this.queryParams.deptId =
          this.queryParams.deptId[this.queryParams.deptId.length - 1];
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeEndTime = [];
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.payId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加缴费信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const payId = row.payId || this.ids;
      getPay(payId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改缴费信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.deptId.length != undefined) {
            this.form.deptId = this.form.deptId[this.form.deptId.length - 1];
          }
          if (this.form.payPicture == "") {
            this.form.payPicture = null;
          }
          this.buttonLoading = true;
          if (this.form.payId != null) {
            updatePay(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addPay(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const payIds = row.payId || this.ids;
      this.$modal
        .confirm('是否确认删除缴费编号为"' + payIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delPay(payIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "money/pay/export",
        {
          ...this.queryParams,
        },
        `pay_${new Date().getTime()}.xlsx`
      );
    },
    haddle(val) {
      this.$modal
        .confirm("确认要查看" + val.remark + "缴费情况吗?")
        .then(() => {
          // console.log(val.payId);
          this.$router.push("/pay/detail/" + val.payId);
        });
    },
  },
};
</script>
<style scoped>
.demo-image__lazy {
  height: 600px;
  overflow-y: auto;
}
.demo-image__lazy .el-image {
  display: block;
  min-height: 200px;
  margin-bottom: 10px;
}
.demo-image__lazy .el-image:last-child {
  margin-bottom: 0;
}
.el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-height: calc(100% - 30px);
  max-width: calc(100% - 30px);
}
.content {
  text-indent: 2em;
}
</style>
